//
//  HomeViewController.m
//  GiftNotes
//
//  Created by wsh on 15/8/11.
//  Copyright (c) 2015年 wsh. All rights reserved.
//

#import "HomeViewController.h"
#import "HomeMenueView.h"
#import "MainBaseViewController.h"
#import "HomeCommonData.h"
#import "HomeBaseViewController.h"
#import "HomeSelectionViewController.h"
#import "HomeHeader.h"

@interface HomeViewController ()<HomeMenuDelegate>

@property (nonatomic, strong)UIBarButtonItem *leftBarButtonItem;
@property (nonatomic, strong)HomeMenueView *menu;
@property (nonatomic, strong)UIButton *button;

@property (nonatomic, strong)HomeBaseViewController *baseVC;
@property (nonatomic, strong)HomeSelectionViewController *selectionVC;

@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationController.navigationBar.barTintColor = [UIColor orangeColor];
    //  改变下一层返回按钮的标题
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:nil action:nil];
    [self.navigationItem setBackBarButtonItem:backItem];

    //  改变返回按钮的颜色
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    
    [self creatMenue];
    [self creatSelectionView];
    
    self.navigationController.navigationBar.backgroundColor = [UIColor colorWithRed:1.0 green:0.585 blue:0.865 alpha:1.0];
    
    _baseVC = [[HomeBaseViewController alloc]init];
    _baseVC.naVC = self.navigationController;
    
    _selectionVC = [[HomeSelectionViewController alloc]init];
    _selectionVC.naVC = self.navigationController;
    [self creatSelectionView];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


#pragma mark - 创建精品页面

- (void)creatSelectionView
{
    _selectionVC.view.frame = CGRectMake(0, 64, kHomeWidth, kHomeHeight - 64);
    [self.view addSubview:_selectionVC.view];
}


#pragma mark - 创建下拉菜单

- (void)creatMenue
{
    
    _button = [UIButton buttonWithType:UIButtonTypeCustom];
    [_button setTitle:@"精选" forState:UIControlStateNormal];
    [_button addTarget:self action:@selector(doTapLeftBarButton:) forControlEvents:UIControlEventTouchUpInside];
    [_button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    
    self.navigationItem.titleView = _button;
    
    NSArray *array = @[@"精选", @"礼物", @"美食", @"数码", @"运动", @"娱乐"];
    _menu = [[HomeMenueView alloc]initWithFrame:CGRectMake(10, 70, 70, 300) array:array];
    _menu.delegate = self;
    
}

- (void)doTapLeftBarButton:(UIBarButtonItem *)sender
{
    if (_menu.isShow) {
        [self.menu menuDismiss];
    } else {
        [self.menu menuShow:self.navigationController];
    }
}


- (void)transportMenuName:(NSString *)name index:(NSInteger)index
{
    [_button setTitle:name forState:UIControlStateNormal];
    
    [_baseVC.view removeFromSuperview];
    [_selectionVC.view removeFromSuperview];
    
    if (index == 0) {
        
        [self creatSelectionView];
        
    } else {
        
        HomeCommonData *data = [HomeCommonData defaultData];
        NSString *url = data.classArray[index - 1];
        _baseVC.url = url;
        [self.view addSubview:_baseVC.view];
        
//        [_baseVC.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1] atScrollPosition:UITableViewScrollPositionTop animated:YES];
        
        _baseVC.view.frame = CGRectMake(0, 64, kHomeWidth, kHomeHeight );
    }
    
    
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
